package com.hc.controller;

import com.hc.common.bean.PageBean;
import com.hc.common.bean.Result;
import com.hc.common.bean.ResultUtil;
import com.hc.constant.RedisConst;
import com.hc.constant.SkuConst;
import com.hc.domain.Sku;
import com.hc.res.CategoryWithSku;
import com.hc.res.Entry;
import com.hc.res.SkuDetail;
import com.hc.res.SkuItem;
import com.hc.res.SpuDetail;
import com.hc.res.UserComment;
import com.hc.service.CommentService;
import com.hc.service.SkuService;
import com.hc.service.SpuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

/**
 * @author hecai
 * @date 2021/12/7 17:41
 */
@Slf4j
@RestController
@RequestMapping("/sku")
public class SkuController {

    @Resource
    private SkuService skuService;

    @PostMapping
    public Result create(@RequestBody Sku sku) {
        boolean res = skuService.save(sku);
        return res ? ResultUtil.success("添加成功") : ResultUtil.fail("添加失败");
    }

    @PutMapping
    public Result updateById(@RequestBody Sku sku) {
        boolean res = skuService.updateById(sku);
        return res ? ResultUtil.success("更新成功") : ResultUtil.fail("更新失败");
    }

    @DeleteMapping("{id}")
    public Result removeById(@PathVariable Serializable id) {
        boolean res = skuService.removeById(id);
        return res ? ResultUtil.success("删除成功") : ResultUtil.fail("删除失败");
    }

    @GetMapping("{id}")
    public Result getById(@PathVariable Serializable id) {
        Sku res = skuService.getById(id);
        return ResultUtil.success("查询成功").setData(res);
    }


    @GetMapping("/v1/getSkusByCategoryId/{id}/{pageNum}/{pageSize}")
    public Result getSkusByCategoryId(@PathVariable("id") Integer cid,
                                      @PathVariable("pageNum") Integer pageNum,
                                      @PathVariable("pageSize") Integer pageSize) {
        PageBean<SkuItem> pageBean = skuService.getSkusByCategoryId(cid, pageNum, pageSize);
        return ResultUtil.success().setData(pageBean);
    }

    @GetMapping("/v1/getSkusByCategoryPid/{pid}/{pageNum}/{pageSize}")
    public Result getSkusByCategoryPid(@PathVariable("pid") Integer pid,
                                       @PathVariable("pageNum") Integer pageNum,
                                       @PathVariable("pageSize") Integer pageSize) {
        PageBean<SkuItem> pageBean = skuService.getSkusByCategoryPid(pid, pageNum, pageSize);
        return ResultUtil.success().setData(pageBean);
    }

    @GetMapping("/v1/getSkusBySpuId/{spuId}/{pageNum}/{pageSize}")
    public Result getSkusBySpuId(@PathVariable("spuId") Integer spuId,
                                 @PathVariable("pageNum") Integer pageNum,
                                 @PathVariable("pageSize") Integer pageSize) {
        PageBean<SkuItem> pageBean = skuService.getSkuBySpuId(spuId, pageNum, pageSize);
        return ResultUtil.success().setData(pageBean);
    }


    /**
     * 首页商品信息
     *
     * @return
     */
    @GetMapping("/v1/indexSkus")
    public Result getIndexSkus() {
        //初始化轮播图
        List<Entry<Integer, String>> carousel = skuService.selectAds(5, SkuConst.Type.CAROUSEL, RedisConst.Index.CAROUSEL, true);
        //初始化分隔
        List<Entry<Integer, String>> divider = skuService.selectAds(4, SkuConst.Type.DIVIDER, RedisConst.Index.DIVIDER, true);
        //初始化广告
        List<Entry<Integer, String>> ad = skuService.selectAds(5, SkuConst.Type.AD, RedisConst.Index.AD, true);
        //初始化推荐
        List<SkuItem> recommend = skuService.selectRecommendOrPromotionSku(5, SkuConst.Type.Recommend, RedisConst.Index.RECOMMEND, true);
        //初始化促销
        List<SkuItem> promotion = skuService.selectRecommendOrPromotionSku(5, SkuConst.Type.Promotion, RedisConst.Index.PROMOTION, true);
        //初始化楼层：类别+sku
        List<CategoryWithSku> categoryWithSku = skuService.selectCategoryWidthSku(4, 5, 8, RedisConst.Index.CATEGORY_WITH_SKU, true);

        return ResultUtil.success().setData("carousel", carousel, "divider", divider, "ad", ad, "recommend", recommend, "promotion", promotion, "categoryWithSku", categoryWithSku);
    }

    @Resource
    private SpuService spuService;

    @Resource
    private CommentService commentService;


    /**
     * 查询指定编号的商品的详情
     *
     * @param skuId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @GetMapping("/skuDetail/{id}/{pageNum}/{pageSize}")
    public Result getSkuDetailById(@PathVariable("id") Integer skuId,
                                   @PathVariable("pageNum") Integer pageNum,
                                   @PathVariable("pageSize") Integer pageSize) {
        //查询sku
        SkuDetail skuDetail = skuService.getSkuDetailBySkuId(skuId);
        if (skuDetail == null) {
            return ResultUtil.fail("没有找到对应的商品信息");
        }
        //查询spu
        SpuDetail spuDetail = spuService.getSpuDetailBySpuId(skuDetail.getSpuId());

        //查询评论
        PageBean<UserComment> userCommentList = commentService.getUserCommentListBySkuId(skuId, pageNum, pageSize);

        return ResultUtil.success().setData("sku", skuDetail, "spuDetail", spuDetail, "userCommentList", userCommentList);
    }

}